<template>
  <div class="container">
    <header-search-form
      :searchFormData="searchForm"
      @search="search"
    />
    <lan-table
      size="small"
      :config="dataTable"
      :tableData="tableData"
      @search="search"
    >
      <template #type="scope">
        <el-input v-model="scope.row.type"></el-input>
      </template>
    </lan-table>

  </div>
</template>

<script>
import lanTable from '../../../components/lanTable';
import HeaderSearchForm from './headerSearchForm.vue';
import moment from 'moment';
import { MomentFormat, SortType } from '../../../constants/common';
import { Message, MessageBox } from 'element-ui';
import { getMqttLogListApi } from '../../../api/mqttLog';
import { parseError } from '../../../utils';

export default {
  components: {
    lanTable,
    HeaderSearchForm
  },
  data() {
    return {
      dataTable: {
        pageSize: 10,
        totalCount: 0,
        currentPage: 1,
        pageSizes: [5, 10, 20, 50, 100],
        sort: {
          orderBy: 'createdAt',
          sortType: SortType.DESC
        },
        columnList: [
          {
            name: 'index',
            label: '',
            type: 'index'
          },
          {
            name: 'objectId',
            label: 'mongodb object id',
            width: 180
          },
          {
            name: 'centerSn',
            label: '主机sn码',
            width: 120
          },
          {
            name: 'topic',
            label: '主题',
            width: 360
          },
          {
            name: 'message',
            label: '内容'
          },
          {
            name: 'createdAt',
            label: '创建时间',
            sortable: true,
            width: 150
          }
        ]
      },
      searchForm: {
        btnLoading: false,
        start: new Date(new Date(new Date().toLocaleDateString()).getTime()),
        end: new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000),
        centerSn: '',
        keyword: ''
      },
      tableData: []
    };
  },
  created() {
    this.search();
  },
  methods: {
    search() {
      this.searchForm.btnLoading = true;
      getMqttLogListApi(
        this.dataTable.currentPage - 1,
        this.dataTable.pageSize,
        this.dataTable.sort.sortType,
        this.dataTable.sort.orderBy,
        this.searchForm.start,
        this.searchForm.end,
        this.searchForm.centerSn,
        this.searchForm.keyword
      )
        .then(({ data, total_count }) => {
          this.dataTable.totalCount = total_count;
          this.tableData = data.map(logList => {
            return {
              objectId: logList.object_id,
              centerSn: logList.center_sn,
              topic: logList.topic,
              message: logList.message,
              createdAt: moment(logList.created_at).format(MomentFormat)
            };
          });
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.searchForm.btnLoading = false));
    }
  }
};
</script>
<style lang="scss" scoped>
.container {
  padding: 10px;
}
</style>
